package com.beck.bee.service;

import com.beck.bee.dao.BaseDao;
import com.beck.bee.domain.Pagination;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cdliubei@jd.com on 3/30/2017.
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    protected Logger logger = LogManager.getLogger(getClass());

    /**
     * 获取DAO操作类
     */
    public abstract BaseDao<T> getDao();

    /**
     * 添加对象
     *
     * @param t
     * @return
     */
    public long insert(T t) {
        if (t == null) {
            return 0;
        }
        return getDao().insert(t);
    }

    /**
     * 删除对象,主键
     *
     * @param key
     * @return
     */
    public int delete(int... key) {
        if (key == null || key.length == 0) {
            return 0;
        }
        return getDao().delete(key);
    }

    /**
     * 删除对象,主键
     *
     * @param key
     * @return
     */
    public int delete(Serializable key) {
        if (key == null) {
            return 0;
        }
        return getDao().delete(key);
    }

    /**
     * 删除对象,条件
     *
     * @param t
     * @return
     */
    public int delete(T t) {
        if (t == null) {
            return 0;
        }
        return getDao().delete(t);
    }

    /**
     * 更新对象,条件主键
     *
     * @param t
     * @return
     */
    public int update(T t) {
        if (t == null) {
            return 0;
        }
        return getDao().update(t);
    }

    /**
     * 查询对象,条件主键
     *
     * @param key
     * @return
     */
    public T select(Serializable key) {
        if (key == null) {
            return null;
        }
        return getDao().select(key);
    }

    /**
     * 查询对象,只要不为NULL与空则为条件
     *
     * @param t
     * @return
     */
    public List<T> select(T t) {
        if (t == null) {
            return new ArrayList();
        }
        return getDao().select(t);
    }

    /**
     * 查询对象总数
     *
     * @param t
     * @return
     */
    public int selectCount(T t) {
        if (t == null) {
            return 0;
        }
        return getDao().selectCount(t);
    }

    /**
     * 获取分页数据
     *
     * @param t
     * @param page
     * @return
     */
    public Pagination selectPage(T t, Pagination page) {
        if (t == null) {
            return page;
        }
        page.setResult(getDao().selectPage(t));
        page.setTotalCount(getDao().selectCount(t));
        return page;
    }

    /**
     * 获取分页数据
     *
     * @param t
     * @return
     */
    public List<T> selectPage(T t) {
        return getDao().selectPage(t);
    }
}
